package com.huidu.bitrade.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huidu.bitrade.dto.MemberWalletModifyDTO;
import com.huidu.bitrade.entity.MemberWallet;
import com.huidu.bitrade.entity.MemberWalletOperLog;
import com.huidu.bitrade.mapper.MemberWalletMapper;
import com.huidu.bitrade.mapper.MemberWalletOperLogMapper;
import com.huidu.bitrade.query.MemberLoanPageQuery;
import com.huidu.bitrade.query.WalletModifyMemberPageQuery;
import com.huidu.bitrade.service.MemberWalletMpService;
import com.huidu.bitrade.service.MemberWalletService;
import com.huidu.bitrade.vo.MemberLoanVO;
import com.huidu.bitrade.vo.MemberWalletModifyVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.security.Security;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class MemberWalletMpServiceImpl extends ServiceImpl<MemberWalletMapper, MemberWallet> implements MemberWalletMpService {

    private final MemberWalletMapper memberWalletMapper;
    private final MemberWalletOperLogMapper memberWalletOperLogMapper;
    private final MemberWalletService memberWalletService;
    @Override
    public IPage<MemberWalletModifyVo> pageList(IPage page, WalletModifyMemberPageQuery memberPageQuery){
        return page.setRecords(memberWalletMapper.getMemberWalletModifyVo(page,memberPageQuery));
    }

    /**
     * 修改用户钱包余额
     * @param modifyDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifyBalance(MemberWalletModifyDTO modifyDTO,MemberWallet memberWallet) throws InvocationTargetException, IllegalAccessException {
        if(Objects.isNull(modifyDTO.getBalance())){
            modifyDTO.setBalance(memberWallet.getBalance());
        }
        if(Objects.isNull(modifyDTO.getFrozenBalance())){
            modifyDTO.setFrozenBalance(memberWallet.getFrozenBalance());
        }
        if(Objects.isNull(modifyDTO.getSystemBalance())){
            modifyDTO.setSystemBalance(memberWallet.getSystemFrozen());
        }
        update(Wrappers.<MemberWallet>lambdaUpdate().eq(MemberWallet::getId,modifyDTO.getWalletId())
                .set(Objects.nonNull(modifyDTO.getBalance()),MemberWallet::getBalance,modifyDTO.getBalance())
                .set(Objects.nonNull(modifyDTO.getFrozenBalance()),MemberWallet::getFrozenBalance,modifyDTO.getFrozenBalance())
                .set(Objects.nonNull(modifyDTO.getSystemBalance()),MemberWallet::getSystemFrozen,modifyDTO.getSystemBalance())
        );
        //添加操作记录
        MemberWalletOperLog memberWalletOperLog = new MemberWalletOperLog();
        BeanUtil.copyProperties(modifyDTO,memberWalletOperLog);
        memberWalletOperLog.setMemberId(memberWallet.getMemberId());
        memberWalletOperLog.setBeforeBalance(memberWallet.getBalance());
        memberWalletOperLog.setBeforeFrozenBalance(memberWallet.getFrozenBalance());
        memberWalletOperLog.setBeforeSystemBalance(memberWallet.getSystemFrozen());
        memberWalletOperLogMapper.insert(memberWalletOperLog);

    }
}
